<template>
  <el-dialog
    title="调用权限"
    :visible="visible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    @close="toClose"
  >
    <el-form
      label-position="right"
      label-width="120px"
      :model="addForm"
      :rules="rules"
      ref="addForm"
      size="small"
    >
      <el-form-item label="调用应用:" prop="app_name">
        <el-input v-model="addForm.app_name"></el-input>
      </el-form-item>
      <el-form-item label="工单前缀:" prop="ticket_sn_prefix">
        <el-input v-model="addForm.ticket_sn_prefix"></el-input>
      </el-form-item>
      <el-form-item label="工作流权限:" prop="workflow_ids">
        <el-select
          v-model="addForm.workflow_ids"
          placeholder="请选择授予的工作流权限"
          class="full-width"
          multiple
        >
          <el-option
            v-for="item in workflowsList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" size="small"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import authManagement from "@/api/systemManagement/workflowsManagement/authManagement/index";
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    id: {
      type: Number,
      required: false
    },
    workflowsList: {
      type: Array,
      required: false
    },
    row: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      rules: {
        app_name: [
          { required: true, message: "请输入调用应用", trigger: "blur" }
        ],
        workflow_ids: [
          {
            required: true,
            message: "请选择授予的工作流权限",
            trigger: "change"
          }
        ]
      },
      addForm: {
        app_name: "",
        ticket_sn_prefix: "",
        workflow_ids: ""
      }
    };
  },
  created() {
    if (this.row.app_name) {
      this.addForm = JSON.parse(JSON.stringify(this.row));
      if (this.row.workflow_ids) {
        this.addForm.workflow_ids = this.row.workflow_ids.split(",");
        this.addForm.workflow_ids = this.addForm.workflow_ids.map(function(
          item
        ) {
          return item * 1;
        });
      }
    }
  },
  methods: {
    toClose() {
      this.$refs["addForm"].resetFields();
    },
    handleClose() {
      this.$emit("update:visible", false);
    },
    submitForm() {
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          let finalForm = {};
          finalForm.workflow_ids = this.addForm.workflow_ids.join(",");
          finalForm.app_name = this.addForm.app_name;
          finalForm.ticket_sn_prefix = this.addForm.ticket_sn_prefix;
          if (this.addForm.id) {
            authManagement
              .editAppToken(this.addForm.id, finalForm)
              .then(res => {
                  this.msgSuccess("修改成功!");
                  this.handleClose();
                  this.$emit("refreshList");
              });
          } else {
            authManagement.addAppToken(finalForm).then(res => {
                this.msgSuccess("添加成功!");
                this.handleClose();
                this.$emit("refreshList");
            });
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>
